<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>new service</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="public/layui/css/layui.css" media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body style="padding:9px 9px 0 9px;overflow:hidden">
<div class="layui-form layui-form-pane" id="ability-form">
    <div class="layui-form-item">
        <label class="layui-form-label">服务编码</label>
        <div class="layui-input-inline">
            <input type="text" name="serviceName" id="serviceName" lay-verify="required|serviceName" autocomplete="off"
                   placeholder="请输入服务编码"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">描述</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入内容" name="description" class="layui-textarea"></textarea>
        </div>
    </div>
    <!--
    <div class="layui-form-item">
        <label class="layui-form-label">是否启用</label>
        <div class="layui-input-block">
            <input type="checkbox" name="state" id="state" lay-filter="state" lay-verify="state" lay-skin="switch"
                   lay-text="启用|关闭">
        </div>
    </div>
    -->
    <div class="layui-form-item">
        <div class="layui-input-block" style="text-align:right;">
            <button class="layui-btn layui-btn-sm" lay-submit="" id="saveBtn" lay-filter="saveBtn">立即提交</button>
        </div>
    </div>
</div>


<script src="public/layui/layui.js" charset="utf-8"></script>
<script src="js/jquery-3.2.1.js"></script>
<script src="js/loader.js"></script>
<script>
    layui.use(['form', 'layedit', 'laydate', 'oka', 'table'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate
            , table = layui.table
            , oka = layui.oka;
        //alert(location.hash);
        //自定义验证规则

        var serviceName = location.hash;
        if (serviceName != "") {
            serviceName = serviceName.substring(1);
            var dom = $("#serviceName")[0];
            dom.style.background="#efefef";
            dom.style.color="#b2b2b2";
            dom.disabled = true;
            dom = null;
            oka.loading.open();
            $.ajax({
                url: "/admin/service/" + serviceName
                , type: "GET"
                , async: true
                , success: function (res) {
                    oka.loading.close();
                    //obj = null;
                    var data = res;
                    if (data.state == "on") {
                        $("#state").prop("checked", true);
                    }
                    $('#ability-form').find('[name]').each(function () {
                        var s = data[$(this).attr('name')];

                        if (s != undefined) {
                            // console.log($(this).attr('name'))
                            $(this).val(s);
                        }

                    });
                    form.render();
                }
            });
        }

        form.verify({
            title: function (value) {
                if (value.length < 5) {
                    return '标题至少得5个字符啊';
                }
            }
            , content: function (value) {
                alert(value)
            }
        });

        form.on('switch(state)', function (data) {
            $("#state").prop("value", this.checked ? 'on' : 'off');
        });

        //监听提交

        form.on('submit(saveBtn)', function (data) {
            oka.loading.open();
            $.ajax({
                url: "/admin/service" + (serviceName != "" ? ("/" + serviceName) : "")
                , type: serviceName != "" ? "PUT" : "POST"
                , dataType: "json"
                , contentType: "application/json"
                , async: false
                , data: JSON.stringify(data.field)
                , success: function (res) {
                    oka.loading.close();
                    if (res.code == "0") {

                        layer.msg('操作失败!!！', {
                            offset: "t",
                            anim: 6
                        });

                    } else {
                        layer.msg('操作成功',{
                            time:600
                        },function(){
                            var ret = {
                                description: data.field.description,
                                serviceName: data.field.serviceName,
                                state: data.field.state ? data.field.state : "off"
                            };
                            if (res.bizDesc) {
                                ret.id = res.bizDesc;
                                ret.insSummary = "0 / 0";
                            }
                            parent.oka.page.ret = JSON.stringify(ret);

                            parent.layer.close(parent.layer.index);
                        });




                    }
                }
            });


            return false;
        });


    });
</script>

</body>
</html>